// manage_careworker.js
var app = getApp()
//常量数据配置
var ConstantData = require('../../../../common/constant_data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    carerWorkerList:[],
    showUpdateTagStatus:false,
    tag:'',
    outpatientKey:'',
    id:0,
    showCancelAgentStatus:false,
    showAddCareWorkerStatus:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var qrcodeContent = options.q
    
    console.log(qrcodeContent)    
    qrcodeContent = decodeURIComponent(qrcodeContent)
    console.log(qrcodeContent)
    if (qrcodeContent && qrcodeContent.indexOf(app.TIME_CARD_OUTPATIENT_QRCODE_URL_PREFIX) == 0) {

      var strLen = app.TIME_CARD_OUTPATIENT_QRCODE_URL_PREFIX.length
      var outpatientKey = qrcodeContent.substring(strLen);
      console.log("---->>outpatientKey=" + outpatientKey)
      
      this.setData({
        outpatientKey: outpatientKey
      })      
    }
    this.getAgentCareWokers()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      id:0,
      showUpdateTagStatus: false,     
      showCancelAgentStatus: false,
      showAddCareWorkerStatus: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 获取可代租赁的护工
   */
  getAgentCareWokers:function() {
    var that = this

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/time_card_patient/careworkers',
        header: header,
        data: {
          key: that.data.outpatientKey
        },
        success: function (res) {
          var result = res.data
          var rst = result.result
          if (rst == 0) {
            //获取成功
            that.setData({
              carerWorkerList: result.data,
            })
          } else {
            if (rst == -130) {
              wx.redirectTo({
                url: '../../../../pages/login/login?pt=outpatient_manage_careworker',
              })
            } else {
              wx.showToast({
                title: result.description + '(' + rst + ')',
              })
            }
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '稍后重试',
          })
        }
      })

    })
    
  },
  /**
   * 弹出更新备注的弹框
   */
  updateTagPopBox:function(){
    var id = e.currentTarget.dataset.id
    this.setData({
      showUpdateTagStatus: true,
      id: id,
    })
  },
  /**
   * 查看护工的租赁记录
   */
  leaseRecord:function(e) {
    var that = this
    var phone = e.currentTarget.dataset.phone
    var key = this.data.outpatientKey
    // wx.navigateTo({
    //   url: '../care_worker_lease_record/care_worker_lease_rcord?phone='+phone+'&key='+key,
    // })    
    wx.navigateTo({
      url: '../manage_detail/manage_detail?phone='+phone+'&key='+key,
    })    
  },
  /**
   * 记录更新的备注信息
   */
  setTag:function(e) {
    this.data.tag = e.detail.value
  },
  /**
   * 取消可代租赁
   */
  cancelAgentPopBox:function(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      showCancelAgentStatus: true,
      id: id,
    })
  },
  /**
   * 取消删除代租权限
   */
  noCancel:function() {
    this.setData({
      showCancelAgentStatus: false,
      id: 0,
    })
  },
  /**
   * 确定删除代租权限
   */
  sureCancelAgent:function() {
    var that = this

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/time_card_patient/delagent',
        header: header,
        data: {
          id: that.data.id,
          key: that.data.outpatientKey
        },
        success: function (res) {
          var result = res.data
          var rst = result.result
          if (rst == 0) {

            that.getAgentCareWokers()

            wx.showToast({
              title: '删除成功',
            })
            that.setData({
              showCancelAgentStatus: false
            })
          } else {
            wx.showToast({
              title: result.description,
            })
          }
        }
      })

    })
    
  },
  /**
   * 取消添加
   */
  cancelAddAgent:function() {
     this.setData({
       showAddCareWorkerStatus:false
     })
  },
  addPopBox:function(){
     this.setData({
       showAddCareWorkerStatus:true
     })
  },
  /**
   * 添加可代租赁人员
   */
  addAgent:function() {
    var that = this
    if(that.data.tag == '') {
      wx.showToast({
        title: '备注不能为空',
      })
      return
    }
    if(that.data.phone == '') {
      wx.showToast({
        title: '手机号不能为空',
      })
      return
    }

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/time_card_patient/addcareworker',
        header: header,
        data: {
          key: that.data.outpatientKey,
          phone: that.data.phone,
          remarks: that.data.tag
        },
        success: function (res) {
          var result = res.data
          var rst = result.result
          if (rst == 0) {
            that.setData({
              showAddCareWorkerStatus: false
            })
            wx.showToast({
              title: '添加成功',
            })
            that.getAgentCareWokers()
          } else {
            wx.showToast({
              title: result.description,
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '稍后重试',
          })
        }
      })

    })

  },
  /**
   * 输入手机号
   */
  setPhone:function(e){
    this.data.phone = e.detail.value
  }
  


})